package com.example.netcomponent.net.utils;

import android.os.Environment;
import android.util.Log;


import com.example.netcomponent.BuildConfig;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @作者 郝彬彬
 * 
 * @时间 2015-2-2 上午10:59:17
 * 
 * @说明 Log工具，支持到具体行
 * 
 * @version 1.0
 */
public class LogUtils {
	private static final boolean DEBUG = BuildConfig.DEBUG;
//	private static final boolean DEBUG = true;
	private static final boolean LOGFILE = true;
	private static final String TAG = "LogUtils.java";
	private static final String path = "/logs/infolog/";
	private static final long max = 2 * 1024;
	/**未捕获的异常*/
	public static final String ERROR = "100";
	/**上传照片日志记录*/
	public static final String UPLOAD_PIC = "101";
	/**记录数据库异常*/
	public static final String ERROR_DB = "102";
	/**下载异常*/
	public static final String ERROR_DOWNFILE = "103";
	/**接口出入参日志记录*/
	public static final String INTERFACE_MODULE = "104";
	public static final String ERROR_CAMERA = "105";

	/**Bugly热更新*/
//	public static final String BUGLY = "105";

	public static ThreadPoolUtils threadPoolUtils=new ThreadPoolUtils(ThreadPoolUtils.Type.CachedThread,1);
	public static void e(String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.e((thisMethodStack.getClassName() + "." + thisMethodStack.getMethodName() + ":" + thisMethodStack.getLineNumber()), result + message);
		}
	}

	public static void e(String MyTAG, String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.e(MyTAG, result + message);
		}
	}

	public static void d(String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.d(thisMethodStack.getClassName() + "." + thisMethodStack.getMethodName() + ":" + thisMethodStack.getLineNumber(), result + message);
		}
	}

	public static void d(String MyTAG, String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.e(MyTAG, result + message);
		}
	}

	public static void i(String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.i(thisMethodStack.getClassName() + "." + thisMethodStack.getMethodName() + ":" + thisMethodStack.getLineNumber(), result + message);
		}
	}

	public static void i(String MyTAG, String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.i(MyTAG, result + message);
		}
	}

	public static void w(String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.w(thisMethodStack.getClassName() + "." + thisMethodStack.getMethodName() + ":" + thisMethodStack.getLineNumber(), result + message);
		}
	}

	public static void w(String MyTAG, String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.w(MyTAG, result + message);
		}
	}

	public static void v(String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.v(thisMethodStack.getClassName() + "." + thisMethodStack.getMethodName() + ":" + thisMethodStack.getLineNumber(), result + message);
		}
	}

	public static void v(String MyTAG, String message) {
		if (DEBUG) {
			String result = "at ";
			StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
			result += thisMethodStack.getClassName() + ".";
			result += thisMethodStack.getMethodName();
			result += "(" + thisMethodStack.getFileName();
			result += ":" + thisMethodStack.getLineNumber() + ")  ";
			Log.v(MyTAG, result + message);
		}
	}

	/**
	 * 
	 * 作者：郝彬彬    
	 * 时间：2015-3-6下午3:02:04 
	 * 参数：@param taskId 任务id（当作文件名）
	 * 参数：@param content 要输出的字符串内容
	 * 返回：void
	 * 描述：将日志存储到sd卡         
	 * 版本：version 1.0
	 */
	public static void saveLogToSD(final String taskId, String content) {
		if (!LOGFILE) {
			return;
		}
		StackTraceElement[] elements = Thread.currentThread().getStackTrace();
		if (elements.length < 3) {
			Log.e(TAG, "Stack to shallow");
		} else {
			final String fullClassName = elements[3].getClassName();
			final String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
			final String methodName = elements[3].getMethodName();
			final int lineNumber = elements[3].getLineNumber();
			// 组装内容
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_MM_dd");
			Date d = new Date();
			String curTime = sdf.format(d);
			final String curDay = sdf2.format(d);
			final StringBuffer sbuf = new StringBuffer("**************" + fullClassName + "." + methodName + "(" + "第" + lineNumber + "行)****************\n");
			sbuf.append(curTime).append("日志输出：\n");
			sbuf.append(content).append("\n");
			threadPoolUtils.execute(new Runnable() {
				@Override
				public void run() {
					if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
						File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
						File savePath = new File(sdCardDir + path + File.separator + curDay + File.separator);
						if (!savePath.exists()) {
							savePath.mkdirs();
						}
						// 写日志到存储卡
						String logName = taskId + ".pa";// 日志文件名称
						File saveFile = new File(sdCardDir + path + File.separator + curDay + File.separator, logName);
						if (!saveFile.exists()) {
							try {
								saveFile.createNewFile();
							} catch (IOException e) {
								LogUtils.w(e.getMessage());
							}
						}
						byte[] cont = sbuf.toString().getBytes();
						FileOutputStream outStream = null;
						try {
							outStream = new FileOutputStream(saveFile, true);
							outStream.write(cont);
						} catch (Exception e) {
							e.printStackTrace();
						} finally {
							if (outStream != null) {
								try {
									outStream.close();
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
			});
		}
	}

	/**
	 * 获取异常信息的堆栈信息
	 * @param ex
	 * @return
	 */
	public static String getExceptionAllinformation(Exception ex) {
		if(ex==null){
			return "ex==null";
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PrintStream pout = new PrintStream(out);
		ex.printStackTrace(pout);
		String ret = new String(out.toByteArray());
		pout.close();
		try {
			out.close();
		} catch (Exception e) {
		}
		return ret;
	}
}